from flask import Blueprint, jsonify, request
from app.services.trafficjam import TrafficJamService

trafficjam_bp = Blueprint('trafficjam', __name__)

@trafficjam_bp.route('/api/traffic-jam/getRoadRank', methods=['GET'])
def get_specific_traffic_jam():
    """
    获取特定类型的道路拥堵数据
    
    Args:
        road_type (int): 道路类型 0-全部道路 1-快速/高速公路 2-普通道路
        time_type (int): 时间类型 0-实时 1-昨天
    """
    try:
        road_type = request.args.get('road_type', '0')
        time_type = request.args.get('time_type', '0')

        print(road_type, time_type)

        if road_type not in ['0', '1', '2'] or time_type not in ['0', '1']:
            return jsonify({"error": "Invalid parameters"}), 400
            
        data = TrafficJamService.get_road_rank_traffic_jam_data(
            road_type=road_type,
            time_type=time_type
        )
        return jsonify(data)
    except Exception as e:
        return jsonify({"error": str(e)}), 500 
    
@trafficjam_bp.route('/api/traffic-jam/getRoadDetail', methods=['GET'])
def get_road_detail():
    """
    获取道路详情

    """
    try:
        line_code = request.args.get('line_code', '')
        road_type = request.args.get('road_type', '0')
        time_type = request.args.get('time_type', '0')
        city_code = request.args.get('city_code', '420100')

        data = TrafficJamService.get_road_detail(
            road_type=road_type,
            time_type=time_type,
            line_code=line_code,
            city_code=city_code
        )
        return jsonify(data)
    except Exception as e:
        return jsonify({"error": str(e)}), 500
